package com.jiang.entity;

/**
 * 区块类
 */
public class Block {
    private int index;//区块在整个链中位置
    private String timestamp;//区块生成的时间戳
    private String hash;//区块的hash
    private String prevHash;//区块的前一个区块的hash
    private int difficulty;//困难数
    private String nonce;//随机数
    private String[] vac;//区块中记录的数据
    private String merkleRoot;//默克尔树根

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getHash() {
        return hash;
    }

    public void setHash(String hash) {
        this.hash = hash;
    }

    public String getPrevHash() {
        return prevHash;
    }

    public void setPrevHash(String prevHash) {
        this.prevHash = prevHash;
    }

    public int getDifficulty() {
        return difficulty;
    }

    public void setDifficulty(int difficulty) {
        this.difficulty = difficulty;
    }

    public String getNonce() {
        return nonce;
    }

    public void setNonce(String nonce) {
        this.nonce = nonce;
    }

    public String getMerkleRoot() {
        return merkleRoot;
    }

    public void setMerkleRoot(String merkleRoot) {
        this.merkleRoot = merkleRoot;
    }

    public String[] getVac() {
        return vac;
    }

    public void setVac(String[] vac) {
        this.vac = vac;
    }

    @Override
    public boolean equals(Object o) {
        if(this == o){
            return true;//直接相同，当然相等
        }
        if(o == null){
            return false;//对比的另一个对象为空，当然为false
        }
        if(getClass() != o.getClass()){
            //如果两个对比的类型不同，就说明不同
            return false;
        }
        Block other = (Block) o;//得到该比较对象
        if(difficulty != other.difficulty){
            return false;//当两个困难度不同，则不同
        }
        if(hash == null){
            if (other.hash != null){
                return false;
            }
        }else if (!hash.equals(other.hash))
            return false;
        if (index != other.index)
            return false;
        if (nonce == null) {
            if (other.nonce != null)
                return false;
        } else if (!nonce.equals(other.nonce))
            return false;
        if (prevHash == null) {
            if (other.prevHash != null)
                return false;
        } else if (!prevHash.equals(other.prevHash))
            return false;
        if (timestamp == null) {
            if (other.timestamp != null)
                return false;
        } else if (!timestamp.equals(other.timestamp))
            return false;
        if (!vac[0].equals(other.vac[0]))
            return false;
        return true;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + difficulty;
        result = prime * result + ((hash == null)? 0:hash.hashCode());
        result = prime * result + index;
        result = prime * result + ((nonce == null) ? 0 : nonce.hashCode());
        result = prime * result + ((prevHash == null) ? 0 : prevHash.hashCode());
        result = prime * result + ((timestamp == null) ? 0 : timestamp.hashCode());
        return result;
    }
}
